package cn.wolfcode.luowowo.mrgsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化监听器
 * ContextRefreshedEvent:spring容器启动,并且完成一些相关初始化操作:IOC DI AOP
 * 在tomcat启动时,就把mysql中指定的copy到redis中
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService strategyDetailService; //mysql中的数据

    @Reference
    private IStrategyStatisVOService strategyStatisVOService; //redis的数据

    //当这个事件发生之后,执行这个方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

            System.out.println("------------------vo-初始化-start--------------------------");

            List<StrategyDetail> list1 = strategyDetailService.list(); //拿到mysql中的所有数据
            StrategyStatisVO vo = null;
            for (StrategyDetail detail : list1) {

                //如果mgrsite重启之后,会将原先在redis的数据进行覆盖,所以需要进行排除
                //如果redis中已经存在,直接跳过
                if(strategyStatisVOService.isVoExist(detail.getId())){
                    continue;
                }

                vo = new StrategyStatisVO();
                BeanUtil.copyProperties(vo,detail); //把mysql中的数据复制到redis中
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            strategyStatisVOService.setStrategyStatisVO(vo); //设置vo对象,k-v对
        }

        System.out.println("------------------vo-初始化-end--------------------------");


        System.out.println("------------------热门推荐初始化-start--------------------------");

        //1.从mysql中查询到所有明细数据
        List<StrategyDetail> list = strategyDetailService.list();

        for (StrategyDetail detail : list) {
            //如果zset已经存在某个column需要直接跳过
            //假设某个column第一次初始化到redis时是100分,后续页面操作加到200嗯
            //如果再次启动,没有进行跳过,那么会重复将mysql中 100 加到redis上,得到300分,此时出问题

            if(strategyStatisVOService.isColumnExistZset(detail.getId(), RedisKeys.STRATEGY_ZSET_HOT.getPrefix())){
                continue;
            }

            //2.将mysql中的数据,评论数+浏览数 弄到remnant的那个zset上
            int scores = detail.getReplynum() + detail.getViewnum();
            strategyStatisVOService.addHotScore(detail.getId(),scores);
        }

        System.out.println("------------------热门推荐初始化-end--------------------------");



        //从mysql中获取所以攻略明细的集合
        System.out.println("-------------------国内热门推荐vo初始化begin------------------");
        for (StrategyDetail detail : list) {

            //如果已经存在某个colum对应的score,就直接跳过
            //原因:假设第一次初始化时,colum对应的score为100,页面操作后为200,当再次启动服务时,score会加到300,此时就不符合实际
            if (strategyStatisVOService.isColumnExistZset(detail.getId(), RedisKeys.STRATEGY_ZSET_CHINA.getPrefix())) {
                continue;
            }
            //将mysql中的数据,收藏+顶,初始化到redis中
            int scores = detail.getFavornum() + detail.getThumbsupnum();
            strategyStatisVOService.addCommendScore(detail.getId(),scores);
        }
        System.out.println("-------------------国内热门推荐vo初始化end------------------");



        //从mysql中获取所以攻略明细的集合
        System.out.println("-------------------国外热门推荐vo初始化begin------------------");
        for (StrategyDetail detail : list) {

            //如果已经存在某个colum对应的score,就直接跳过
            //原因:假设第一次初始化时,colum对应的score为100,页面操作后为200,当再次启动服务时,score会加到300,此时就不符合实际
            if (strategyStatisVOService.isColumnExistZset(detail.getId(), RedisKeys.STRATEGY_ZSET_ABROAD.getPrefix())) {
                continue;
            }
            //将mysql中的数据,收藏+顶,初始化到redis中
            int scores = detail.getFavornum() + detail.getThumbsupnum();
            strategyStatisVOService.addCommendScore(detail.getId(),scores);
        }
        System.out.println("-------------------国外热门推荐vo初始化end------------------");


    }
}
